import store from "@/store"
import { defineStore } from "pinia"
import { getUUID, getLoginStatus, addOrUpdateQuser, getBindedQuser } from "@/api/qyyjt"
import type { basic_info, qLoginResponseData } from "@/api/qyyjt/types/quser"

export const useQyyjtStore = defineStore("qyyjt", {
  state: () => ({
    quser: {
      basic_info: {} as basic_info | any,
      accessToken: "",
      refreshToken: ""
      // qtoken:{} as qToken | any
    }
  }),
  actions: {
    async loginStatus(data: { uuid: string }) {
      let resp = await getLoginStatus(data)
      return resp.data.data
    },
    bindQuser(data: qLoginResponseData) {
      console.log("登陆成功后quser", data)
      Object.assign(this.quser, data.token)
      this.quser.basic_info = data.basic_info
      this.addOrUpdateBinding()
      // this.quser.qtoken = data.token
    },
    async addOrUpdateBinding() {
      await addOrUpdateQuser(this.quser)
    },
    async getQuser() {
      let resp = await getBindedQuser()
      if (resp.code === 200) {
        this.quser.basic_info = resp.data.basic_info
        this.quser.accessToken = resp.data.accessToken
        this.quser.refreshToken = resp.data.refreshToken
      }
    }
  }
})
